package com.cssw.fyzb.tps.api.im;

import com.cssw.fyzb.tps.service.im.MessageService;
import com.cssw.fyzb.tps.service.im.common.ImHttpComm;
import com.cssw.fyzb.tps.service.im.common.ImMessage;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author Jian Qin.
 */
@Function(description = "单发单聊位置消息", parameters = {
        @Parameter(name = "syncOtherMachine", type = Type.String, description = "消息是否同步到From_Account，1 是 2 否", required = false),
        @Parameter(name = "from_Account", type = Type.String, description = "消息发送方 UserID", required = false),
        @Parameter(name = "to_Account", type = Type.String, description = "消息接收方 UserID，可以是列表，以','分割"),
        @Parameter(name = "msgLifeTime", type = Type.Integer, description = "消息离线保存时长，单位秒，0只发在线用户", required = false),
        @Parameter(name = "forbidBeforeSendMsgCallback", type = Type.String, description = "1 禁止发消息前回调", required = false),
        @Parameter(name = "forbidAfterSendMsgCallback", type = Type.String, description = "1 禁止发消息后回调", required = false),
        @Parameter(name = "desc", type = Type.Integer, description = "地理位置描述信息"),
        @Parameter(name = "latitude", type = Type.String, description = "纬度"),
        @Parameter(name = "longitude", type = Type.String, description = "经度"),
})
@Component
public class SendLocationMsg implements Action {
    @Autowired
    private MessageService messageService;

    @Override
    public Response execute(Request request) throws Exception {
        ImMessage.ImLocationMessage locationMessage = new ImMessage.ImLocationMessage();
        locationMessage.setSyncOtherMachine("1".equals(request.getString("syncOtherMachine")));
        locationMessage.setFrom_Account(request.getString("from_Account"));
        locationMessage.setTo_Account(request.getString("to_Account").split(","));
        locationMessage.setMsgLifeTime(request.getInteger("msgLifeTime"));
        locationMessage.setForbidBeforeSendMsgCallback("1".equals(request.getString("forbidBeforeSendMsgCallback")));
        locationMessage.setForbidAfterSendMsgCallback("1".equals(request.getString("forbidAfterSendMsgCallback")));
        locationMessage.setDesc(request.getString("desc"));
        locationMessage.setLatitude(Double.parseDouble(request.getString("latitude")));
        locationMessage.setLongitude(Double.parseDouble(request.getString("longitude")));

        ImHttpComm.ImResult result = null;
        if(locationMessage.getTo_Account().length > 1){
            result = messageService.batchSendMsg(locationMessage.toJson());
        }else{
            result = messageService.sendMsg(locationMessage.toJson());
        }
        if(result.getErrorCode() == 0){
            return Response.success()
                    .put("msgTime", result.getString("MsgTime"))
                    .put("msgKey", result.getString("MsgKey"));
        }else{
            return Response.fail(result.getErrorInfo(), result.getErrorCode());
        }
    }
}
